<?php
/**
 * Created by PhpStorm.
 * User: Frings
 * Date: 2016/9/5
 * Time: 9:54
 */

use yii\helpers\Url;

$web = Url::base();
?>

<script type="application/javascript">
    var to_institution_sel;
    $(function () {
        var institutions = <?=json_encode($to_institutions)?>;
        to_institution_sel = $('#to_institution_sel').treeAutocomplete({
            defaultText: '收卡机构',
            source: institutions,
            onSelected: function (element) {
//                alert(element.getValue());
            }
        });

        <?php
        if($item != null){
        ?>
        to_institution_sel.setValue('<?=$item['to_institution_code']?>');
        to_institution_sel.isDisabled = true;
        <?php
        }
        ?>

        //选择模板
        $("#card_tpl_sel").change(function () {
            var obj = $(this);
            var data = {};
            data.id = obj.val();
            if (data.id == '') {
                return false;
            }
            loading.show();
            requestJson("get-card-tpl-handler", data, true, function (response) {
                if (response.status == 0) {
                    var d = response.data;
                    $("#name").val(d.name);
                    $("#description").val(d.description);
                    $("#integral").val(d.integral);
                    loading.show();
                }
            });
            //goods
            requestHtml("card-tpl-part-view-goods", data, true, function (responseHtml) {
                $("#goods_table").find("tbody").html(responseHtml);
                loading.hide();
            });
        });

        //选择我的卡片
        $("#my_card_sel").change(function () {
            var obj = $(this);
            var data = {};
            data.id = obj.val();
            if (data.id == "" || data.id == null) {
                return false;
            }
            loading.show();
            requestJson("get-card-handler", data, true, function (response) {
                if (response.status == 0) {
                    var d = response.data;
                    $("#name").val(d.name);
                    $("#description").val(d.description);
                    $("#integral").val(d.integral);
                    $("#start_index").val(d.last_index);
                    data.integral = d.integral;
//                    $("#start_index").attr("disabled", "disabled");
                    //goods
                    requestHtml("card-part-view-goods", data, true, function (responseHtml) {
                        $("#goods_table").find("tbody").html(responseHtml);
                        loading.hide();
                    });
                }
            });
        });

        $("#activationVoucher").bootstrapSwitch('state', false);
//        //初始化checkbox样式
//        $('.square-blue input').iCheck({
//            checkboxClass: 'icheckbox_square-blue',
//            radioClass: 'iradio_square-blue',
//            increaseArea: '20%' // optional
//        });


//        $('input:checkbox[name=goods]').on('ifClicked', function (event) {
////            $(this).is(':checked');
//            checkSort(this);
//        });
    });

    function checkSort(obj) {
        var sortTextBox = $(obj).parent().prev().find('input');
        if ($(obj).is(':checked')) {
            sortTextBox.removeAttr("disabled");
        } else {
            sortTextBox.attr("disabled", "disabled");
        }
    }

    function calcCount() {
        var startindex = $('#start_index').val();
        var endindex = $('#end_index').val();

        if (isNaN(startindex) || isNaN(endindex)) {
            $('#quantity').html(0);
            return;
        }
        var r = endindex - startindex + 1;
        if (r < 0) {
            $('#quantity').val(0);
            return;
        }
        $('#quantity').val(r);
    }

    function calcEndIndex() {
        var startindex = $('#start_index').val();
        var count = $('#quantity').val();
        if (!isNaN(startindex) && !isNaN(count)) {
            var endindex = parseInt(startindex) + parseInt(count) - 1;
            $('#end_index').val(endindex);
        }
    }

    function intChange(filter) {
        if (event.keyCode == 13 || filter != undefined) {
            var data = {};
            data.id = $("#id").val();
            data.integral = $("#integral").val();

            var goods = {};
            goods.list = [];
            var goodsChecks = $("#goods_table").find('input:checkbox[name=goods]:checked');
            for (var i = 0; i < goodsChecks.length; i++) {
                var g = {};
                g.code = $(goodsChecks[i]).val();
                g.sort = $(goodsChecks[i]).parent().prev().find('input').val();
                goods.list.push(g);
            }

            data.list = goods.list;
            //goods
            loading.show();
            requestHtml("card-part-view-goods-intetral", data, true, function (responseHtml) {
                $("#goods_table").find("tbody").html(responseHtml);
                loading.hide();
            });
        }
    }
    function int_filter() {
        var hide_integral = $("#hide_integral").val();
        var integral = $("#integral").val();
        if (hide_integral != "") {
            if (hide_integral = integral) {
                alert("不能筛选小于当前积分的商品");
                return false;
            }
        }
        intChange(true);
    }
</script>
<div class="form-horizontal" id="modaledit_submit_form">
    <div class="form-group" style=" margin-top: 5px">
        <label class="control-label col-lg-2" for="exampleInputPassword2">收卡机构：</label>
        <div class="col-lg-10">
            <div id="to_institution_sel"></div>
        </div>
    </div>
    <?php
    if (count($cardtpls) > 0 && $item == null) {
        ?>
        <div class="form-group" style=" margin-top: 5px">
            <label class="control-label col-lg-2" for="exampleInputPassword2">卡片模板：</label>
            <div class="col-lg-10">
                <select id="card_tpl_sel" class="form-control">
                    <option value="">选择模板</option>
                    <?php
                    foreach ($cardtpls as $cardtpl) {
                        ?>
                        <option value="<?= $cardtpl['id'] ?>"><?= $cardtpl['name'] ?></option>

                        <?php
                    }
                    ?>
                </select>
            </div>
        </div>
        <?php
    }
    ?>
    <?php
    if (count($my_cards) > 0 && $item == null) {
        ?>
        <div class="form-group" style=" margin-top: 5px">
            <label class="control-label col-lg-2" for="exampleInputPassword2">我的卡片：</label>
            <div class="col-lg-10">
                <select id="my_card_sel" class="form-control">
                    <option value="">选择卡片</option>
                    <?php
                    foreach ($my_cards as $my_card) {
                        ?>
                        <option value="<?= $my_card['id'] ?>"><?= $my_card['name'] ?></option>
                        <?php
                    }
                    ?>
                </select>
            </div>
        </div>
        <?php
    }
    ?>
    <div class="form-group" style=" margin-top: 5px">
        <input type="hidden" id="id" name="id" class="form-control" value="<?= $item != null ? $item['id'] : '' ?>">
        <label class="control-label col-lg-2" for="exampleInputPassword2">卡片名称：</label>
        <div class="col-lg-10">
            <input type="text" id="name" name="name" class="form-control"
                   value="<?= $item != null ? $item['name'] : '' ?>">
        </div>
    </div>
    <div class="form-group" style=" margin-top: 5px">
        <label class="control-label col-lg-2" for="exampleInputPassword2">卡片描述：</label>
        <div class="col-lg-10">
        <textarea id="description" name="description" class="form-control"
                  rows="3"><?= $item != null ? $item['description'] : '' ?></textarea>
        </div>
    </div>
    <div class="form-group" style=" margin-top: 5px">
        <label class="control-label col-lg-2" for="exampleInputPassword2">单证开始：</label>
        <div class="col-lg-2">
            <input type="number" id="start_index" name="start_index" class="form-control"
                   value="<?= $item != null ? $item['start_index'] : '' ?>" <?= $item != null ? 'disabled="disabled"' : '' ?>
                <?php if ($current_user_id != '0') echo 'disabled="disabled"' ?> oninput="calcCount()">
        </div>
        <label class="control-label col-lg-2" for="exampleInputPassword2">单证数量：</label>
        <div class="col-lg-2">
            <input type="number" id="quantity" name="quantity" class="form-control"
                   oninput="calcEndIndex()" <?= $item != null ? 'disabled="disabled"' : '' ?>
                   value="<?= $item != null ? $item['end_index'] - $item['start_index'] + 1 : '' ?>">
        </div>

        <label class="control-label col-lg-2" for="exampleInputPassword2">单证结束：</label>
        <div class="col-lg-2">
            <input type="number" id="end_index" name="end_index" class="form-control"
                   value="<?= $item != null ? $item['end_index'] : '' ?>" <?= $item != null ? 'disabled="disabled"' : '' ?>
                   oninput="calcCount()">
        </div>
    </div>
    <div class="form-group" style=" margin-top: 5px">
        <label class="control-label col-lg-2" for="exampleInputPassword2">价值积分：</label>
        <div class="col-lg-10">
            <div class="input-group m-bot15">
                <input type="hidden" id="hide_integral" value="<?= $item != null ? $item['integral'] : '' ?>">
                <input type="number" id="integral" name="integral" class="form-control"
                       value="<?= $item != null ? $item['integral'] : '' ?>"
                       onkeypress="intChange()">
                <span class="input-group-btn">
                                                <button class="btn btn-primary" type="button"
                                                        onclick="int_filter()">筛选</button>
                                              </span>
            </div>

        </div>
    </div>
    <?php
    if ($item == null) {
        ?>
        <div class="form-group" style=" margin-top: 5px">
            <label class="control-label col-lg-2" for="exampleInputPassword2">激活单证：</label>
            <div class="col-lg-4">
                <div class="switch">
                    <input type="checkbox" id="activationVoucher"><span
                        class="btn-clipboard popovers" data-trigger="hover"
                        data-placement="right"
                        data-content=""
                        data-original-title="提示"></span>
                </div>
            </div>
        </div>
        <?php
    }
    ?>
    <div class="form-group" style=" margin-top: 5px;">
        <div class="col-lg-12">
            <table class="table table-bordered table-hover table-condensed" id="goods_table">
                <thead class="nowrap">
                <tr>
                    <th class="col-lg-1">排序(数字大的排前面)</th>
                    <th class="col-lg-1"></th>
                    <th class="col-lg-2">产品编码</th>
                    <th class="col-lg-6">产品名</th>
                    <th class="col-lg-2">价格</th>
                </tr>
                </thead>
                <tbody>
                <?php
                if ($goods != null && $goods != '') {
                    for ($i = 0; $i < count($goods); $i++) {
                        ?>
                        <tr>
                            <td><input name="sort" type="number" <?= $goods[$i]['sort'] == null ? 'disabled' : '' ?>
                                       value="<?= $goods[$i]['sort'] == null ? 0 : $goods[$i]['sort'] ?>"></td>
                            <td>
                                <input onclick="checkSort(this)" type="checkbox"
                                       name="goods" <?= $goods[$i]['sort'] != null ? 'checked="checked"' : '' ?>
                                       value="<?= $goods[$i]['code'] ?>">
                            </td>
                            <td name="code"><?= $goods[$i]['code'] ?></td>
                            <td name="name"><?= $goods[$i]['name'] ?></td>
                            <td name="selling_price"><?= $goods[$i]['selling_price'] ?></td>
                        </tr>
                        <?php
                    }
                }
                ?>
                </tbody>
            </table>
        </div>
    </div>
</div>